<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['index',"xieyi","help","test"];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        //轮播图
        $banner = Db::name('banner')->where('status', 1)->order('sort DESC')->select();
        foreach ($banner as $k => $v) {
            $banner[$k]['imageurl'] = $this->request->domain() . $v['imageurl'];
        }
        $this->success('请求成功', $banner);
    }

    /**
     * 搜索主播功能
     *
     */
    public function search_anchor()
    {
        $search = $this->request->post('search');
        $loginuser = $this->auth->getUser();
        if ($search) {
            $user = Db::name('user')->where("group_id", 1)->where(function ($query) use ($search) {
                $query->whereOr("id", $search);
                $query->whereOr("nickname", "LIKE", "%{$search}%");
            })
                ->field('id,group_id,nickname,up_switch,anchor_status,avatar,gender')
                ->select();
            foreach ($user as $k => $v) {
                $user[$k]['config'] = Db::name('anchor_config')->where('anchor_id', $v['id'])->field('place_price,gen_price')->find();
            }
            if (empty($user)) {
                $this->error('搜索的主播不存在');
            }
        } else {
            $this->error('请填写搜索内容');
        }

        foreach ($user as $k=>$v){
            $is_follow = Db::name('user_follow')
                ->where('r_user_id',$loginuser['id'])
                ->where('l_user_id',$v['id'])
                ->where('status',1)
                ->where('genre',1)->find();
            if($is_follow){
                $user[$k]['is_follow'] = 1;
            }else{
                $user[$k]['is_follow'] = 0;
            }

        }
        $this->success('成功', $user);

    }

    public function test()
    {
        $params = $this->request->post();
        extract($params);
//        var_dump($id);die;

//        $search = $this->request->param('search', "");
//        $user = Db::name('user')->where("group_id", 1)->where(function ($query) use ($search) {
//            $query->whereOr("id", $search);
//            $query->whereOr("nickname", "LIKE", "%{$search}%");
//        })->select();
//        $this->success(1, $user);
    }
    public function help()
    {
        $help = Db::name('config')->where(['name'=>'help'])->value('value');
        $this->success('成功', $help);
    }


    //获取协议
    public function xieyi()
    {
        $id = $this->request->param('id', "");
        if (is_numeric($id)) {
            $where['id'] = $id;
        } else {
            $where['name'] = [
                'LIKE' => "%" . $id . "%"
            ];
        }
        $list = Db::name('agreement')->where($where)->find();
        $this->success("协议", $list);
    }
}
